<template>
    <div class="cmt-container">
        <h3>发表评论</h3>
        <hr>
        <textarea placeholder="请输入要BB的内容(最多BB120个字)" v-model="msg"></textarea>
        <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>
        <div class="cmt-list">
            <div class="cmt-item" v-for="(item,i) in comments" :key="item.add_time">
                <div class="cmt-title">第{{i+1}}楼&nbsp;&nbsp;用户:{{item.username}}&nbsp;&nbsp;发表时间:{{ item.add_time | dataFormat }}</div>
                <div class="cmt-body">{{ item.content === "undefined" ? '此用户没有填写评论' : item.content }}</div>
            </div>
        </div>
        <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
    </div>
</template>

<script>
    import { Toast } from 'mint-ui'

    export default {
        data(){
            return{
                pageindex:1,     //默认为第一页数据
                msg:'',     //评论输入的内容
                comments: [        //所有的评论数据
                    { username:'匿名用户', add_time:'2019-01-22T09:59:28.000Z', content:'哎 这是要干什么啊'},
                    { username:'带带大师兄', add_time:'2019-01-22T10:59:28.000Z', content:'首先你告诉我和谁恰 去哪里恰'},
                    { username:'匿名用户', add_time:'2019-01-22T11:59:28.000Z', content:'我问你一问三不知'},
                    { username:'匿名用户', add_time:'2019-01-22T12:59:28.000Z', content:'真的是很无语'},
                ]
            }
        },
        created(){ this.getComments(); },
        methods:{getComments(){
                this.$http.get('/api/getcomment/' + this.id + '?pageindex=' + this.pageindex).then(result => {
                    if(result.body.status === 0 ){
                        //每当获取新数据 就拼接到旧数据上
                        this.comments = this.comments.concat(result.body.message);
                        Toast('加载成功');
                    }else{
                        Toast('加载失败');
                    }
                })
            },
            getMore(){
            this.pageindex++;
            this.getComments();
            },
            postComment(){
            //校验是否为空
                if(this.msg.trim().length === 0){
                    return Toast('评论不能为空!');
                }

            //参数1 请求的url地址   参数2 提交给服务器的数据对象   参数3 定义提交的表单中数据的格式 emulateJSON: true
            this.$http.post('api/postcomment/' + this.id, {
                content: this.msg.trim()
            } ).then(function (result) {
                if(result.body.status === 0 ){
                    //1. 拼接一个评论对象
                    var cmt ={ user_name: '匿名用户', add_time: Date.now(), content: this.msg.trim()};
                    this.comments.unshift(cmt);
                    this.msg = '';
                }
            })
            }
        },
        props:["id"]
    }
</script>

<style lang="scss" scoped>
    .cmt-container{
        h3{
            font-size: 18px;
        }
        textarea{
            font-size: 14px;
            height: 85px;
            margin: 0;
        }
        .cmt-list{
            margin: 5px 0;
            .cmt-item{
                .cmt-title{
                    background-color: #999;
                    font-size:13px;
                    line-height: 30px;
                }
                .cmt-body{
                    font-size:15px;
                    line-height: 35px;
                    text-indent: 2em;
                }
            }
        }
    }
    .mint-button--primary{
        background-color: #242424;
    }
</style>
